package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.OrganizationFormMapper;
import com.hbnu.system.model.entity.OrganizationForm;
import com.hbnu.system.service.OrganizationFormService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class OrganizationFormServiceImpl extends ServiceImpl<OrganizationFormMapper, OrganizationForm> implements OrganizationFormService {
    @Override
    public boolean addOrganizationForm(OrganizationForm organizationForm) {
        OrganizationForm activityType1 = super.getOne(Wrappers.lambdaQuery(OrganizationForm.class)
                .eq(OrganizationForm::getOrganizationFormName,organizationForm.getOrganizationFormName()));
        if(StrUtil.isEmpty(organizationForm.getOrganizationFormName())||StrUtil.isBlank(organizationForm.getOrganizationFormName())){
            throw new BaseException("该组织形式名称为空");
        }
        if(!Objects.isNull(activityType1)){
            throw new BaseException("该组织形式名称已存在");
        }
        return baseMapper.insert(organizationForm)==1;
    }

    @Override
    public List<OrganizationForm> getOrganizationFormList() {
        List<OrganizationForm> depts = baseMapper.selectList(Wrappers.lambdaQuery(OrganizationForm.class));
//        List<String> result = new ArrayList<>();
//        for (Dept dept : depts) {
//            result.add(dept.getDeptName());
//        }
        return depts;
    }

    @Override
    public boolean updateOrganizationForm(OrganizationForm organizationForm) {

        if(StrUtil.isEmpty(organizationForm.getOrganizationFormName())||StrUtil.isBlank(organizationForm.getOrganizationFormName())){
            throw new BaseException("该组织形式名称为空");
        }
        if(baseMapper.updateById(organizationForm)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteOrganizationForm(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<OrganizationForm> getByOrganizationFormName(String organizationFormName) {
        LambdaQueryWrapper<OrganizationForm> queryWrapper = Wrappers.lambdaQuery(OrganizationForm.class);

        // 如果 organizationFormName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(organizationFormName)) {
            queryWrapper.like(OrganizationForm::getOrganizationFormName, organizationFormName);
        }

        return baseMapper.selectList(queryWrapper);
    }
}
